/**
 * 装车控制 -  道闸参数设置 - 弹窗
 * @author xiaoxujs
 */

import { FC, useEffect, useState } from "react";
import { IIYckzZyLoadControlParamSetConfigQueryVOS } from "../../../../../type/remoteControl/loadingControlList";
import { Form, Input, InputNumber, message, Modal, Radio } from "antd";
import { postYckzZyLoadControlBarrierConfigUpdate } from "../../../api";


const DaozhaSetModule: FC<{
    open: boolean,
    handleSetCallback: () => void;
    clickData?: IIYckzZyLoadControlParamSetConfigQueryVOS,
    clickId?: string;
    clickType?: 0 | 1
}> = ({
    open,
    handleSetCallback,
    clickData,
    clickId,
    clickType
}) => {
        const [form] = Form.useForm<IIYckzZyLoadControlParamSetConfigQueryVOS>();
        const [loading, setLoading] = useState(false);
        useEffect(() => {
            if (clickData) {
                form.setFieldsValue(clickData)
            }
        }, [clickData, form])
        const handleOk = async () => {
            const values = await form.validateFields();
            console.log(clickType)
            console.log(clickId)
            if (clickId) {
                setLoading(true);
                const { code, msg } = await postYckzZyLoadControlBarrierConfigUpdate({
                    ...values,
                    barrierId: clickData?.barrierId,
                    type: clickType,
                    id: clickId
                }) as { code: number; msg: string };
                if (code === 20000) {
                    form.resetFields();
                    handleSetCallback();
                } else {
                    message.error(msg)
                }
                setLoading(false)

            }

        };
        const handleCancel = () => {
            handleSetCallback();
        };
        const titleValue = () => {
            let str = '';
            if (clickType === 0) {
                str = "设置道闸入口参数"
            } else if (clickType === 1) {
                str = "设置道闸出口参数"
            }
            return str;
        }
        return <Modal title={titleValue()} confirmLoading={loading} open={open} onOk={handleOk} onCancel={handleCancel}>
            <Form
                labelCol={{ span: 6 }}
                wrapperCol={{ span: 16 }}
                form={form}
            >
                <Form.Item<IIYckzZyLoadControlParamSetConfigQueryVOS>
                    label="IP地址"
                    name="barrierIp"
                    rules={[{ required: true, message: '请输入IP地址!' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item<IIYckzZyLoadControlParamSetConfigQueryVOS>
                    label="端口号"
                    name="barrierPort"
                    rules={[{ required: true, message: '请输入端口号!' }]}
                >
                    <InputNumber max={65535} style={{ width: '100%' }} />
                </Form.Item>
                <Form.Item<IIYckzZyLoadControlParamSetConfigQueryVOS>
                    label="用户名"
                    name="barrierUser"
                    rules={[{ required: true, message: '请输入用户名!' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item<IIYckzZyLoadControlParamSetConfigQueryVOS>
                    label="密码"
                    name="barrierPassword"
                    rules={[{ required: true, message: '请输入密码!' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item<IIYckzZyLoadControlParamSetConfigQueryVOS>
                    label="设备品牌"
                    name="barrierBrand"
                    rules={[{ required: true, message: '请选择设备品牌!' }]}
                >
                    <Radio.Group >
                        <Radio value='海康威视'>海康威视</Radio>
                        <Radio value='臻识科技'>臻识科技</Radio>
                    </Radio.Group>
                </Form.Item>
                <Form.Item<IIYckzZyLoadControlParamSetConfigQueryVOS>
                    label="输出编号"
                    name="outLane"
                    rules={[{ required: true, message: '请输入IP地址!' }]}
                >
                    <InputNumber style={{ width: '100%' }} min={0} max={10000} />
                </Form.Item>
            </Form>
        </Modal>
    }

export default DaozhaSetModule;